import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import button.ActionButton;
import button.ClearButton;
import button.EndButton;
import button.LineButton;
import button.StartButton;
import common.ButtonClickListener;
import common.Common;
import common.DrawPanel;
import common.ImagePanel;


public class MyCanvas extends Applet implements MouseListener{

	//鼠标左键与右键的座标
	private int leftMousePoint=-1;
	private int rightMousePoint=-1;
	
	private int x=-1;
	private int y=-1;
	private Point p=null;
	
	private ActionButton bStart=new StartButton();
	private ImagePanel startImg=new ImagePanel();
	
	private ActionButton bEnd=new EndButton();
	private Button bClear=new ClearButton();
	private Button bLine=new LineButton();
	
	private static final Log log=LogFactory.getLog(MyCanvas.class);
	
	public MyCanvas(){
		setSize(900, 600);
		setBackground(Color.cyan);
		
		add(bStart);
		add(bEnd);
		add(bClear);
		add(bLine);
		add(startImg);
		add(new ImagePanel());
		
		add(new DrawPanel(new StartButton().getImage()));
		
		bStart.addMouseListener(new ButtonClickListener(bStart));
		bEnd.addMouseListener(new ButtonClickListener(bEnd));
		bClear.addMouseListener(new ButtonClickListener(bClear));
		bLine.addMouseListener(new ButtonClickListener(bLine));
		
		addMouseListener(this);
	}

	public void mouseClicked(MouseEvent e) {
	}
	public void setMouseLeft(){
		this.leftMousePoint=1;
		this.rightMousePoint=-1;
	}
	public void setMouseRight(){
		this.rightMousePoint=1;
		this.leftMousePoint=-1;
	}
	
	public void mousePressed(MouseEvent e) {
		Button b=Common.getButtonClick();
		if(b==null){
			return;
		}
		p=e.getPoint();
		x=e.getX();
		y=e.getY();
		if(b instanceof ActionButton){
			ActionButton bu=(ActionButton)b;
			if (bu.getStatus() == ActionButton.DragStatus.Ready) {
				bu.setStatus(ActionButton.DragStatus.Dragging);
				bu.setMouseStartposition(e.getPoint());
				bu.getImageStartposition().setLocation(bu.getImagePosition().getLocation());
            }
			startImg.setImagePosition(e.getPoint());
			Common.setButtonClick(bu);
		}
		if(b instanceof LineButton){
			LineButton lb=(LineButton)b;
			if(lb.getTimes()>1){
				lb.setStart(lb.getEnd());
				log.debug("line:["+lb.getStart()[0]+","+lb.getStart()[1]+"]-["+lb.getEnd()[0]+","+lb.getEnd()[1]+"]");
			}else{
				lb.setStart(new int[]{x,y});
				log.debug("line:["+x+","+y+"][]");
			}
			lb.setEnd(new int[]{x,y});
			lb.setTimes(lb.getTimes()+1);
			Common.setButtonClick(lb);
		}
		if (b instanceof ClearButton) {
			this.leftMousePoint=-1;
			this.rightMousePoint=-1;
			if(b!=null&&ClearButton.NAME.equals(b.getName())){
				repaint();
			}
			return;
		}
		if(e.getModifiers()==InputEvent.BUTTON1_MASK){
			this.setMouseLeft();
		}else if(e.getModifiers()==InputEvent.BUTTON3_MASK){
			this.setMouseRight();
		}
		repaint();
	}

	public void mouseReleased(MouseEvent e) {
		Button b=Common.getButtonClick();
		if(b==null){
			return;
		}
		p=e.getPoint();
		x=e.getX();
		y=e.getY();
		if(b instanceof ActionButton){
			ActionButton bu=(ActionButton)b;
			if (bu.getStatus() == ActionButton.DragStatus.Dragging) {
				bu.setStatus(ActionButton.DragStatus.Ready);
            }
			Common.setButtonClick(bu);
		}
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		
	}

	@Override
	public void paint(Graphics g) {
		Button b=Common.getButtonClick();
		if(b==null){
			return;
		}
		if(b instanceof ActionButton){
			ActionButton ab=(ActionButton)b;
			Image img=ab.getImage();
			startImg.setImage(img);
			startImg.repaint();
//			add(new ImagePanel(img,p));
//			g.drawImage(img, (int) ab.getImagePosition().getX(), (int) ab.getImagePosition().getY(), this);
			//g.drawImage(img, x-10, y-10, 50, 50, this);
		}
		if(b instanceof LineButton){
			LineButton lb=(LineButton)b;
			if(lb.getEnd()!=null&&lb.getTimes()>2){
				g.drawLine(lb.getStart()[0], lb.getStart()[1], lb.getEnd()[0], lb.getEnd()[1]);
			}
		}
	}

	@Override
	public void update(Graphics g) {
		if(leftMousePoint==1||rightMousePoint==1){
			paint(g);
		}else{
			super.update(g);
		}
	}

}
